package sample.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.core.widget.NestedScrollView
import androidx.lifecycle.lifecycleScope
import abc.reflect.AESUtils
import com.netease.core.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.script.tes.CrashUtils
import kotlinx.coroutines.script.tes.DiskLogUtils
import kotlinx.coroutines.script.tes.FileUtils
import kotlinx.coroutines.script.tes.colorInt
import kotlinx.coroutines.script.tes.phoneDefaultDisplayPoint
import kotlinx.coroutines.script.tes.setOnClickCallback
import kotlinx.coroutines.script.tes.startAnimationScaleKick
import kotlinx.coroutines.script.tes.systemWindowInsetsTop
import kotlinx.coroutines.script.tes.toTime
import kotlinx.coroutines.script.tes.toast
import kotlinx.coroutines.withContext
import kotlin.math.max
import kotlin.math.min

class LogActivity : BaseActivity() {

    @SuppressLint("CutPasteId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.loga)
        val top = systemWindowInsetsTop
        var offset = top
        val x = phoneDefaultDisplayPoint.x
        val y = phoneDefaultDisplayPoint.y
        if ((x == 1080 && y == 1920) || (x == 720 && y == 1280) ||
            (x == 1920 && y == 1080) || (x == 1280 && y == 720) ||
            (x == 960 && y == 540) || (x == 540 && y == 960)
        ) {
            offset = 0
        }
        findViewById<View>(R.id.root).setPadding(offset, 0, 0, 0)
        findViewById<View>(R.id.fitsSys).setPadding(top / 2, top / 2, top / 2, top / 2)
        findViewById<View>(R.id.back).setOnClickCallback {
            finish()
        }
        findViewById<View>(R.id.other).setOnClickCallback {
            it.context.startActivity(Intent(it.context, OtherActivity::class.java))
        }
        findViewById<View>(R.id.export).setOnClickCallback {
            toast("请全屏截图该页面发送反馈给作者")
        }
        findViewById<View>(R.id.recordLogEnable).setOnClickListener {
            it.startAnimationScaleKick()
            Properties.setRecordLogImage(true)
            updateUI()
        }
        findViewById<View>(R.id.recordLogDisable).setOnClickListener {
            it.startAnimationScaleKick()
            Properties.setRecordLogImage(false)
            updateUI()
        }
        updateUI()

        lifecycleScope.launch(Dispatchers.Main) {
            val decrypted = withContext(Dispatchers.IO) {
                val text = FileUtils.syncRead(DiskLogUtils.logText)
                AESUtils.decrypt(AESUtils.K, text)
            }

            findViewById<TextView>(R.id.data).text = decrypted
            findViewById<TextView>(R.id.data).alpha = 0.4f
            delay(300)
            findViewById<NestedScrollView>(R.id.scrollView).smoothScrollTo(0, Int.MAX_VALUE, 300)

            delay(400)
            val file = CrashUtils.getScreenshotErrorStoppedByDisk()
            val bitmap = FileUtils.readBitmapFromDisk(file)
            if (bitmap != null) {
                findViewById<ImageView>(R.id.errorImageStopped).also {
                    val bitmapW = OtherActivity.ViewHolder.bw ?: bitmap.width.also { OtherActivity.ViewHolder.saveBw(it) }
                    val bitmapH = OtherActivity.ViewHolder.bh ?: bitmap.height.also { OtherActivity.ViewHolder.saveBh(it) }
                    it.updateLayoutParams<ConstraintLayout.LayoutParams> {
                        val w = max(bitmapW, bitmapH)
                        val h = min(bitmapW, bitmapH)
                        dimensionRatio= "H,${w}:${h}"
                    }
                    it.setImageBitmap(bitmap)
                    it.isVisible = true
//                    it.setOnLongClickListener {
//                        displayInitBmp(it as ImageView?)
//                        true
//                    }
                }
                findViewById<TextView>(R.id.errorTime).text = file.lastModified().toTime("yyyy/MM/dd HH:mm:ss")
            }
        }
    }

//    private fun displayInitBmp(view: ImageView?) {
//        lifecycleScope.launch(Dispatchers.Main) {
//            val initBmp = FileUtils.readBitmapFromDisk(GComponents.snapshot)
//            view?.setImageBitmap(initBmp)
//        }
//    }

    private fun updateUI() {
        val recordLogImage = Properties.recordLogImage
        findViewById<TextView>(R.id.recordLogEnable).setTextColor(if (recordLogImage) "#ffd700".colorInt else "#99ffffff".colorInt)
        findViewById<TextView>(R.id.recordLogDisable).setTextColor(if (!recordLogImage) "#ffd700".colorInt else "#99ffffff".colorInt)
    }
}